package com.earth.s.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cybermkd.kit.MongoQuery;
import com.earth.c.controller.BaseController;
import com.earth.c.util.EncryptUtil;
import com.jfinal.kit.StrKit;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
public class TilesController extends BaseController {

	
	//https://www.cnblogs.com/icesnow521/p/9566142.html
	
	@RequestMapping(value = "/tiles/{resource}/{z}/{x}/{y}.png", produces = MediaType.IMAGE_JPEG_VALUE)
	@ResponseBody
	public BufferedImage getPNG(@PathVariable("resource") String resource, @PathVariable("z") String z,
			@PathVariable("x") String x, @PathVariable("y") String y, @RequestParam("token") String token)
			throws IOException {

		// 未授权
		if (StrKit.isBlank(token)) {
			return null;
		}
		MongoQuery query=new MongoQuery();
		try {
			query.use(resource).byId(EncryptUtil.md5(String.format("%s-%s-%s-%s",z,x,y,token))).getDocument().get("tile");
			

			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return ImageIO.read(new FileInputStream(new File("D:/test.jpg")));
	}
	

	@RequestMapping(value = "/tiles/{resource}/{z}/{x}/{y}.jpg", produces = MediaType.IMAGE_JPEG_VALUE)
	@ResponseBody
	public BufferedImage getJPG(@PathVariable("resource") String resource, @PathVariable("z") String z,
			@PathVariable("x") String x, @PathVariable("y") String y, @RequestParam("token") String token)
			throws IOException {

		// 未授权
		if (StrKit.isBlank(token)) {
			return null;
		}

		return ImageIO.read(new FileInputStream(new File("D:/test.jpg")));
	}

}
